<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>修改图书</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
    <link rel="stylesheet" href="css/add.css" />
  </head>

  <body>
    <div class="nav">
      <img src="pic/smarmot.png" alt="" />
      <span class="blog-title">我的博客系统</span>
      <div class="space"></div>
      <a class="nav-span" href="blog_list.html">主页</a>
      <a class="nav-span" href="blog_edit.html">写博客</a>
    </div>

    <div class="container">
      <div class="form-inline">
        <h2 style="text-align: left; margin-left: 10px">
          <span>修改用户信息</span>
        </h2>
      </div>

      <form id="updateUser" enctype="multipart/form-data">
        <div class="form-group">
          <label for="userName">用户名称:</label>
          <input
            type="text"
            class="form-control"
            id="userName"
            name="userName"
          />
        </div>

        <div class="form-group">
          <label for="githubUrl">GitHub地址:</label>
          <input
            type="text"
            class="form-control"
            id="githubUrl"
            name="githubUrl"
          />
        </div>

        <div class="form-group">
          <label for="avatar">上传头像:</label>
          <input type="file" class="form-control" id="avatar" name="avatar" />
        </div>

        <div class="form-group" style="text-align: right">
          <button
            type="button"
            class="btn btn-info btn-lg"
            onclick="updateUser()"
          >
            确定
          </button>
          <button
            type="button"
            class="btn btn-secondary btn-lg"
            onclick="javascript:history.back()"
          >
            返回
          </button>
        </div>
      </form>
    </div>
    <script src="js/jquery.min.js"></script>
    <script src="js/common.js"></script>
    <script>
      function updateUser() {
        // 获取表单数据
        var formData = new FormData($("#updateUser")[0]);

        // 将头像文件添加到 FormData 对象中
        var avatarFileInput = $("#avatar")[0];
        formData.append("avatar", avatarFileInput.files[0]);

        // 发送更新用户信息的请求
        $.ajax({
          type: "POST",
          url: "/user/updateUser",
          data: formData,
          processData: false,
          contentType: false,
          success: function (result) {
            console.log(result);
            if(result.code == "SUCCESS" && result.data == "") {
              alert("更新成功");
              location.href = "blog_list.html";
            }
          },
          error: function (error) {
            // 处理错误的回调
            console.error(error);
          },
        });
      }

      getUserInfo();
      function getUserInfo() {
        $.ajax({
          type: "get",
          url: "/user/getUserInfo",
          success: function (result) {
            if (result.code == "SUCCESS" && result.data != null) {
              var user = result.data;
              $("#userName").val(user.userName);
              $("#githubUrl").val(user.githubUrl);
            }
          },
        });
      }
    </script>
  </body>
</html>
